<?php
include_once "function_admin_manage.php";

function GetUserPassWd()
{
	print "Please input your passWord:";
	//fscanf ( STDIN , "%d\n" , $str1 ); 
	$str1 = trim(fgets(STDIN));
	print "Please reinput your passWord :";
	$str2 = trim(fgets(STDIN));
	if ($str1 != $str2){
		print "Error: input your password!\n";
		return NULL;
	}
	else
		return $str1;
}

function GetUserOldPassWd()
{
	print "Please input your old passWord:";
	$str = trim(fgets(STDIN));
	return $str;
}

if ($argc != 2){
	print "Usage:\n $argv[0] name \n";
	exit(0);
}	
$user = $argv[1];

$ret_old = GetUserOldPassWd();
$old_pw = GetCryptText($ret_old);
$xml_pd = GetXmlSysAdminByName($user);

if ($xml_pd['PassWord'] != $old_pw) {	
	print "Error: input old password!\n";
	exit(0);
}	

$ret_pw = GetUserPassWd();
if ($ret_pw == NULL)
	exit(0);

$in_array['Name'] = $user;
$in_array['PassWord'] = GetCryptText($ret_pw);
$in_array["Enabled"] = 1;

SetXmlOneSysAdminPassWord($in_array);
ApplyAdminToSystem_Edit($in_array["Name"], $in_array["PassWord"],$in_array["Enabled"]);

$XmlFile = "/ldisk/config/config.xml";
$query_string = "/MINI/ACCOUNT/SYSTEM/ADMINISTRATORS/ADMINISTRATOR[@Name='". $user. "']";

$Ret_Array = array();
if ( !file_exists($XmlFile)) {
	return NULL;
}
$Ret_Array["doc"] = new DOMDocument();
$Ret_Array["doc"]->formatOutput = true;
$Ret_Array["doc"]->preserveWhiteSpace = false;
$Ret_Array["doc"]->load($XmlFile);
$Ret_Array["xpath"] = new Domxpath($Ret_Array["doc"]);

$doc = $Ret_Array["doc"];
$xpath = $Ret_Array["xpath"];

$results = $xpath->query($query_string);
$result = $results->item(0);	

$Attr_array = array_keys($in_array);
foreach ($Attr_array as $Attr) {	
	$result->setAttribute($Attr, $in_array[$Attr]);
}
$doc->save($XmlFile);	

?>
